Hey there.
In this lecture we're going to take a look at typecasting.
The purpose of typecasting is to specify or convert the type of an expression.
When that expression is too generic, and an expression is simply just a value or something that executes
and returns a value.
So this would include variables, parameters, and return values from functions.
There may be times when an automatically inferred type is too generic, and you want to narrow it down
to be a more specific type.
And in order to do this, we have to use typecasting.
However, in order to typecast the original type and the type of the typecast have to be related.
So let's go ahead and take a look at some examples for typecasting.
So I'm going to create a table.
I'm just going to call it my table.
And inside we're going to have a key value pair.
I'll just call it names.
And it's going to be equal to a table.
Now let's say I wanted to be able to say that only this table can contain strings.
I don't want to be able to insert anything else into this table.
Well, in its current state, when I go to insert something into this table, like my table names and
I can insert a number, we're not going to have any issues here.
It's not going to underline anything red for us.
However, if I copy this I'll call this my table two.
And then I put a typecast on this table to make it only contain, um, values of a specific type.
then we're going to get a red underline.
If we try to insert a value that isn't of that type.
So in order to create a type cast we need to use two colons instead of one.
So this is not type annotation but this is type casting.
So we do two colons after we assign the value.
And then we can put a table in here.
And inside this table we can define that.
This names table is going to contain values of type string.
And that means now our number one here is going to be underlined.
Red type number could not be converted into string.
And that's because we've explicitly defined here.
Or we've cast the type of string to be contained within our names table.
Now another important thing to note is that type casts are also automatically checked by the type engine,
meaning you can't cast unrelated types.
So as an example, if I created a variable, I'll call it sum num and assign it a number like one.
And then I try to create another variable.
I'll call it value one and set it equal to some numb.
I wouldn't be able to do something like set it to a boolean or a string, because those types are unrelated.
I could set it to something to be something more generic, like any.
So if I do this with a typecast, this is okay, we're not going to get any red underline here.
And now that means value one exists as any because we've casted it to be of type any from some num.
But we can't do something like this.
If I created a value, set it equal to some num, and then I try to cast the type of like boolean on
it.
We're going to get a red underline here.
And that's because we cannot cast number into boolean because the types are unrelated.
So again this is the type checking engine checking for us and seeing hey you can't do this.
These types are unrelated.
Now another example we're going to take a look at is this function right here.
And you might remember this function from our previous lectures.
So basically we had this function here that took two numbers.
And one number was a and it has to be a number.
And the other parameter was b.
And this was an optional parameter.
We don't know if uh whoever uses this function is going to pass a number to be or not.
So we set a default right here.
However, the type checking engine isn't smart enough to realize that we've set a default for B, so
it says right here type number.
Question mark could not be converted into number.
So we're going to have this error here.
And you might be wondering well how do we get rid of this error.
Well we can use typecasting in order to fix this error because number question mark is an optional parameter,
right.
It is still of the type number.
And that means number question mark and number are related types.
That means we can cast B to be of type number.
And that means this error here would disappear.
So what I could do right here is I could put two colons and type cast number for B here for this return
value.
And now as you can see that red underline has completely disappeared.
And this is because we've specified the type more clearly for the type checking engine.
We've narrowed it down to be of the type number.
Now these were just some basic examples of type casting.
And you you probably won't use them that often, but they do exist to help to make your code more readable
to both you and the type checking engine.
Solving errors like the one we just did here.
So again, you won't likely use type casting that often, but it is very useful in those situations
where you have to use it like right here.
Otherwise, I hope this lecture helped you out a little bit with type casting, and I'll see you in
the next lecture.